package src.main.java.org.collection.map;

import src.main.java.org.oop.enumDemo.B;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

/*
* map的遍历方式
* */
public class Test2 {
    public static void main(String[] args) {

        Map<String,Double> map = new HashMap<>();
        map.put("蜘蛛精",162.8);
        map.put("蜘蛛精",169.8);
        map.put("紫霞仙子",165.8);
        map.put("至尊宝",169.7);
        map.put("牛魔王",183.6);
        System.out.println(map);

        //1.先获取Map集合全部的键，再通过遍历键来找值
        Set<String> keys = map.keySet();
        for (String key : keys) {
            double value = map.get(key);
            System.out.println(key+"==>"+value);
        }
        System.out.println("===========");
        //2.把键值对看成一个整体遍历
            //调用entrySet方法，将Map集合转换成键值对类型的Set集合
        Set<Map.Entry<String, Double>> entries = map.entrySet();
        for (Map.Entry<String, Double> entry : entries) {
            String key = entry.getKey();
            Double value = entry.getValue();
            System.out.println(key+"==>"+value);
        }
        System.out.println("===========");
        //3.Lambda表达式实现
        map.forEach((k,v)->{
            System.out.println(k+"==>"+v);
        });
        //原始如下
        /*map.forEach(new BiConsumer<String, Double>() {
            @Override
            public void accept(String s, Double aDouble) {
                System.out.println(s+"-->"+aDouble);
            }
        });*/
    }
}
